<template>
  <!-- 传入内容 插槽内容 会循环多次 有多少节点 就循环多少次 -->
  <!-- 作用域插槽 slot-scope="obj" 接收传递给插槽的数据   data 每个节点的数据对象-->
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <el-col>
      <!-- 左侧内容 -->
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 放置下拉菜单 -->
          <el-dropdown @command="opterDepts">
            <!-- 内容 -->
            <span>操作
              <i class="el-icon-arrow-down" />
            </span>
            <!-- 具名插槽 -->
            <el-dropdown-menu slot="dropdown">
              <!-- 下拉选项 -->
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="emit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    opterDepts(type) {
      if (type === 'add') {
        this.$emit('addDeparts', this.treeNode)
        // 添加部门
      } else if (type === 'emit') {
        // 编辑
        this.$emit('emitDepts', this.treeNode)
      } else if (type === 'del') {
        // 删除
        this.$confirm('确定要删除吗').then(() => {
          // console.log(this.treeNode)
          // props 传参是异步的
          return delDepartments(this.treeNode.id)
        }).then(() => {
          this.$message.success('删除成功')
          this.$emit('delDepts') // 触发父组件的delDepts方法
        }).catch((err) => {
          this.$message.error(err)
        })
      }
    }
  }
}
</script>

<style>
</style>
